package zyw;

import aiUtils.AiUtils;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import javax.swing.*;
import javax.swing.plaf.FontUIResource;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Enumeration;
import java.awt.Font;

public class AiMonthly {

    /** 修改全局 Swing 控件的字体 */
    private static void setUIFont(FontUIResource font) {
        Enumeration<Object> keys = UIManager.getDefaults().keys();
        while (keys.hasMoreElements()) {
            Object key = keys.nextElement();
            Object value = UIManager.get(key);
            if (value instanceof FontUIResource) {
                UIManager.put(key, font);
            }
        }
    }

    public static void main(String[] args) throws IOException {
        //字体大小
        setUIFont(new FontUIResource("Microsoft YaHei", Font.PLAIN, 24));

        //日期格式转换
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        Date startDate = new Date();
        String startTime = formatter.format(startDate);

        // 使用 JFileChooser 让用户选择 Excel 文件
        JFileChooser fileChooser = new JFileChooser();
        fileChooser.setDialogTitle("请选择 Excel 文件");
        fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
        fileChooser.setFileFilter(new javax.swing.filechooser.FileNameExtensionFilter("Excel 文件 (*.xlsx)", "xlsx"));

        int userSelection = fileChooser.showOpenDialog(null);

        if (userSelection != JFileChooser.APPROVE_OPTION) {
            System.out.println("用户取消选择文件，程序终止。");
            return; // 如果用户点击取消或关闭对话框，直接退出
        }

        File selectedFile = fileChooser.getSelectedFile();
        String inputFilePathA = selectedFile.getAbsolutePath();
        System.out.println("选择的文件路径: " + inputFilePathA);

//        String inputFilePathA = "D:\\work\\临时文件\\zyw\\工作量\\要改.xlsx";

        AiUtils aiUtils = new AiUtils();

        int sheetA = 0;

        // 加载A文件
        try (FileInputStream fis = new FileInputStream(inputFilePathA);
             Workbook workbook = new XSSFWorkbook(fis)) {
            // 获取sheet索引从0开始
            Sheet sheet = workbook.getSheetAt(sheetA);

            // 跳过表头开始遍历
            for (int rowIndex = 1; rowIndex <= sheet.getLastRowNum(); rowIndex++) {
                Row row = sheet.getRow(rowIndex);
                if (row != null) {
                    // 假设目标是第三列（索引为2，因为索引从0开始）
                    Cell projectNameCell = row.getCell(2, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);
                    Cell workTypeCell = row.getCell(4, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);
                    Cell workCell = row.getCell(6, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);

                    String projectName = projectNameCell.toString();
                    System.out.println(projectName);
                    String workType = workTypeCell.toString();

                    System.out.println("\n\n-----------" + rowIndex + "-----------");

                    // 记录开始时间
                    long startTimeL = System.currentTimeMillis();

                    //核心程序
                    String workResult = aiUtils.askAi(projectName,workType);
                    workCell.setCellValue(workResult);
                    System.out.println(workResult);

                    // 记录结束时间
                    long endTimeL = System.currentTimeMillis();
                    // 计算耗时（毫秒）
                    long duration = endTimeL - startTimeL;
                    System.out.println("\n请求耗时: " + (duration / 1000.0) + " 秒）");

                    if (rowIndex%5 == 0){
                        //每10条保存一次
                        try (FileOutputStream fos = new FileOutputStream(inputFilePathA)) {
                            workbook.write(fos);
                            System.out.println("-------保存-------");
                        }
                    }
                }
            }

            // 将修改后的workbook写入文件
            try (FileOutputStream fos = new FileOutputStream(inputFilePathA)) {
                workbook.write(fos);
            }
        }

        // 输出提示信息
        System.out.println("处理完成，文件已保存到：" + inputFilePathA);

        Date endDate = new Date();
        String endTime = formatter.format(endDate);
        System.out.println("开始时间: " + startTime);
        System.out.println("结束时间: " + endTime);
        System.out.println("程序执行完毕，按任意键退出...");
        System.in.read(); // 等待用户按键
    }

}
